package com.ntschy.common.core.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.PageData;

import java.util.List;

/**
 * web层通用数据处理
 * @author ezreal_geng
 */
public class BaseController<T> {

    /**
     * 响应返回结果
     *
     * @param rows 影响行数
     * @return 操作结果
     */
    protected AjaxResult<String> toAjax(int rows) {
        return rows > 0 ? AjaxResult.success("操作成功") : AjaxResult.error(HttpStatus.ERROR,"操作失败");
    }

    protected AjaxResult<String> toAjax(int rows, String msg, String errMsg) {
        return rows > 0 ? AjaxResult.success(msg) : AjaxResult.error(HttpStatus.ERROR,errMsg);
    }

    protected AjaxResult toAjax(Object o, String errMsg) {
        return o != null ? AjaxResult.success(o) : AjaxResult.error(HttpStatus.ERROR,errMsg);
    }

    public AjaxPageResult<T> buildPageResult(List<T> data, Page<T> page){
        PageData pageData = new PageData(page.getPageNum(), page.getPageSize(), page.getTotal());
        return AjaxPageResult.success(data, pageData);
    }
}
